package com.link.base.base.accnt.service;

import com.link.base.base.accnt.dao.mybatis.mapper.SaleRegionMapper;
import com.link.base.base.accnt.model.SaleRegion;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
/**
 * Copyright  hande Company. All Rights Reserved.
 *
 * @author  unknwon
 * @date:   2018/11/7 17:00
 * @version 1.0
 */

@Service
public class SaleRegionServiceImpl extends BasicServiceImpl<SaleRegion> implements SaleRegionService {

    @Resource
    private SaleRegionMapper saleRegionMapper;

    @Override
    public BasicMapper<SaleRegion> getBasicMapper() {
        return saleRegionMapper;
    }

    @Override
    public List<SaleRegion> queryByAcctIdPage(SaleRegion entity) throws Exception {
        return saleRegionMapper.queryByAcctIdPage(entity);
    }

    /**
     * 通过区域编码查询数据
     * @param regionCode 区域编码
     * @return
     * @throws Exception
     */
    @Override
    public SaleRegion queryByCode(String regionCode) throws Exception {
        SaleRegion saleRegion = null;
        if (StringUtils.isBlank(regionCode)) {
            throw new ServiceException("ACCT-041");
        }
        SaleRegion example = new SaleRegion();
        example.setRegionCode(regionCode);
        example.setPageFlag(false);
        List<SaleRegion> list = queryByExamplePage(example);
        if (list.size() == 1) {
            saleRegion = list.get(0);
        } else if (list.size() > 1) {
            throw new ServiceException("ACCT-042",regionCode);
        } else {
            throw new ServiceException("ACCT-043",regionCode);
        }
        return saleRegion;
    }
}
